<template>
  <el-dialog
    :title="!dataForm.memorizingId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="55%"
    top="25vh"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="50px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="歌曲" prop="song">
        <el-input v-model="dataForm.song" placeholder="歌曲" />
      </el-form-item>
      <el-form-item label="链接" prop="link">
        <el-input v-model="dataForm.link" placeholder="链接" />
      </el-form-item>
      <el-form-item label="初识" prop="acquaintance">
        <el-input v-model="dataForm.acquaintance" placeholder="初识" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import reqApi from '@/api/music'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        memorizingId: '',
        song: '',
        link: '',
        acquaintance: ''
      },
      dataRule: {
        song: [
          { required: true, message: '歌曲不能为空', trigger: 'blur' }
        ],
        link: [
          { required: true, message: '链接不能为空', trigger: 'blur' }
        ],
        acquaintance: [
          { required: true, message: '初识不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.memorizingId = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.memorizingId) {
          reqApi.getInfo(this.dataForm.memorizingId).then(({ data }) => {
            if (data) {
              this.dataForm.song = data.musicMemorizing.song
              this.dataForm.link = data.musicMemorizing.link
              this.dataForm.acquaintance = data.musicMemorizing.acquaintance
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (!this.dataForm.memorizingId) {
            reqApi.save(this.dataForm).then(({ data }) => {
              if (data) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          } else {
            reqApi.update(this.dataForm).then(({ data }) => {
              if (data) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        }
      })
    }
  }
}
</script>
